﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class FrmGopBan : Form
    {
        BanBUS bnBUS = new BanBUS();
        ChiTietHoaDonBUS ctBUS = new ChiTietHoaDonBUS();
        HoaDonBUS hdBUS = new HoaDonBUS();
        public FrmGopBan()
        {
            InitializeComponent();
            LoadDS_BanCuVaMoi();
        }
        public void LoadDS_BanCuVaMoi()
        {
            dtgrwMaBanGop.DataSource = bnBUS.LayBanTheoTrongNgayChuaThanhToan();
            dtgrwMaBanGop.Columns["MaBan"].HeaderText = "Mã Bàn";
            dtgrwMaBanGop.Columns.Remove("ThoiGianDat");
            dtgrwMaBanGop.Columns.Remove("TrangThai");
            dtgrwMaBanGop.Columns.Remove("KhuVuc");
            cbBanMoi.DataSource = bnBUS.LayBanTheoTrongNgayChuaThanhToan();
            cbBanMoi.DisplayMember = "MaBan";
            cbBanMoi.ValueMember = "MaBan";
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            int dem = 0;
            BanDTO bn = new BanDTO();
            foreach (DataGridViewRow row in dtgrwMaBanGop.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    dem++;
                    string id = row.Cells[1].Value.ToString();
                    ChiTietHoaDonDTO ct1 = new ChiTietHoaDonDTO();
                    ChiTietHoaDonDTO ct2 = new ChiTietHoaDonDTO();
                    if (row.Cells[1].Value.ToString() != cbBanMoi.SelectedValue.ToString())
                    {
                        ct1.MaBan = id;
                        ct1.MaHD = ctBUS.LayMaHDChiTietHoaDon(id);
                    }
                    ct2.MaBan = cbBanMoi.SelectedValue.ToString();
                    ct2.MaHD = ctBUS.LayMaHDChiTietHoaDon(cbBanMoi.SelectedValue.ToString());
                    List<ChiTietHoaDonDTO> listMonAn1 = ctBUS.LayMaMonAnBietMaHD_MaBan(ct1);
                    List<ChiTietHoaDonDTO> listMonAn2 = ctBUS.LayMaMonAnBietMaHD_MaBan(ct2);
                    foreach (ChiTietHoaDonDTO cthd2 in listMonAn2)
                    {
                        foreach (ChiTietHoaDonDTO cthd1 in listMonAn1)
                        {
                            if (cthd1.MaMonAn == cthd2.MaMonAn)
                            {
                                if (cthd1.MaBan != cbBanMoi.SelectedValue.ToString())
                                {
                                    cthd2.SoLuong = cthd2.SoLuong + cthd1.SoLuong;
                                    cthd2.TongTien = cthd2.DonGia * cthd2.SoLuong;
                                    ctBUS.CapNhatChiTietHoaDon(cthd2, cthd2.MaMonAn);
                                    ctBUS.XoaChiTietHoaDon(cthd1);
                                }
                            }
                            else
                            {
                                ctBUS.CapNhatMaBanTheoHD(cthd1, cthd2);                          
                            }
                        }
                        //cap nhat hoa don
                        HoaDonDTO hd = new HoaDonDTO();
                        hd.MaHD = cthd2.MaHD;
                        hd.TenUser = User.TenUser;
                        hd.TinhTrangHD = "";
                        hd.GioRa = "";
                        hd.ThanhTien = ctBUS.TinhTongTienChiTietHoaDon(cthd2.MaHD);
                        hdBUS.CapNhatHoaDon(hd);
                        hdBUS.XoaHoaDon(ct1.MaHD);
                    }
                    //xoa hd cu
                    if (ct1.MaBan != cbBanMoi.SelectedValue.ToString())
                    {
                        //hdBUS.XoaHoaDon(ct1.MaHD);
                    }
                    //cap nhat trang thai ban                    
                    bn.MaBan = id;
                    bn.TrangThai = "Trống";
                    bnBUS.CapNhatBan(bn);
                }
            }
            if (dem < 2)
            {
                MessageBox.Show("Chọn ít Nhất 2 Bàn Để Gộp!");
                return;
            }
            //cap nhat trang thai ban
            bn.MaBan = cbBanMoi.SelectedValue.ToString(); ;
            bn.TrangThai = "Bận";
            bnBUS.CapNhatBan(bn);
            MessageBox.Show("Gộp Bàn Thành Công!");
            LoadDS_BanCuVaMoi();
            return;
        }

        private void FrmGopBan_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.frm_manhinhchinh.sub_mn_GopBan.Checked = false;
        }
    }
}
